# -*- coding: utf-8 -*-
"""
Created on Tue Mar 29 15:36:27 2022

@author: Administrator
"""

import pandas as pd
from sklearn.tree import DecisionTreeClassifier
import sklearn.preprocessing as sp
from sklearn.model_selection import train_test_split
import numpy as np
from sklearn import datasets

# iris数据集
# iris=datasets.load_iris()
# X=iris.data
# y=iris.target
#泰坦尼克号
X=np.load('E:/PROJECT_Dynasty2023/project_-dynasty2023/Project_MachineLearning/教材/titanic_feature.npy')
y=np.load('E:/PROJECT_Dynasty2023/project_-dynasty2023/Project_MachineLearning/教材/titanic_target.npy')
#数据集划分 
X_tr,X1_te,Y_train,Y_test= train_test_split(X, y,test_size=0.3,random_state=50)
result_train=[]
result_test=[]
for i in range(1,20):
    clf=DecisionTreeClassifier(max_depth=i)
    clf.fit(X_tr, Y_train)
    score_test=clf.score(X1_te,Y_test)
    score_train=clf.score(X_tr,Y_train)#树的深度变化
    #准确度
    result_test.append(score_test)
    result_train.append(score_train)
#检测数据与预测数据比较
#输出准确率，召回率，F特征值和支持度
from sklearn.tree import export_graphviz    # 导入的是一个函数
with open('E:/graphviz/Graphviz/iris无参数.dot', 'w', encoding='utf-8') as f:
    f = export_graphviz(clf,  out_file=f,filled=True,rounded=True)
print(result_test)
import matplotlib.pyplot as plt
krange=range(1,20,1)
fig=plt.figure()
plt.plot(krange,result_train,color='k',label='train')
plt.plot(krange,result_test,color='r',label='test')
plt.legend()
plt.show()
